package apptoko.com.validator.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import apptoko.com.validator.AbstractValidator;

import apptoko.com.R;
;

public class UsernameSpecialCharacterValidator extends AbstractValidator {
	
	private int mErrorMessage = R.string.validator_username_special_character;
	
	
	public UsernameSpecialCharacterValidator(Context c) {
		super(c);
	}
	
	@Override
	public boolean isValid(String value) {
		Pattern p;
		Matcher m;
		p=Pattern.compile("\\w(.*)",Pattern.CASE_INSENSITIVE);//start with letter
		m = p.matcher(value);
		if(m.matches()){
			if (value.contains("__") || value.contains("..")||value.contains("@@")) {// not contains more than __ or ..
				System.out.println("Step 1: fail");
				return false;
			} else {
				value = value.replace("_", "");
				System.out.println("New string: "+value);
				value = value.replace(".", "");
				System.out.println("New string: "+value);
//				p = Pattern.compile("@+", Pattern.CASE_INSENSITIVE);
//				m = p.matcher(value);
//				int count=0;
//				while (m.find()){
//					count++;
//				}
//				if (count<=1) {
//					System.out.println("1 @ or less");
//				} else {
//					System.out.println("more than 1 @");
//				}
				p = Pattern.compile("\\w+");
				m = p.matcher(value);
				if(m.matches()){
					System.out.println("valid");
					return true;
				}else{
					System.out.println("contains invalid characters");
					return false;
				}
			}
		}else{
			System.out.println("invalid first character");
			return false;
		}
	}

	@Override
	public String getMessage() {
		return mContext.getString(mErrorMessage);
	}

}
